package visaoInterface;

import dao.DaoPessoa;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import modelo.dominio.Cliente;
import modelo.dominio.Pessoa;

public class RegistroCliente extends javax.swing.JDialog {
    
    //define o modelo da tabela de registro de cliente na interface.
    DefaultTableModel tmRegistroCliente = new DefaultTableModel(null, new String[]{"Nome","Cpf","Telefone"} ) {
        @Override
        public boolean isCellEditable(int row, int col)
        {
            return false;
        } 
    };

    /** Creates new form RegistroCliente */
    public RegistroCliente(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /**
     * mostra na tabela os dados do cliente na tabela.
     * @param pessoas vetor de pessoas para mostrar os dados.
     */
    public void mostraPesquisa(Vector<Pessoa> pessoas){
        String[] linha = new String[] {null,null,null};
        
        //zera o jTable
        while(tmRegistroCliente.getRowCount()>0){
            tmRegistroCliente.removeRow(0);
        }   
        
            for (int i=0 ; i<pessoas.size() ; i++) {
            tmRegistroCliente.addRow(linha);
            tmRegistroCliente.setValueAt(pessoas.get(i).getNome(),i,0);
            tmRegistroCliente.setValueAt(pessoas.get(i).getCpf(),i,1);
            tmRegistroCliente.setValueAt(pessoas.get(i).getTelefone(),i,2);
            
            } 
        if (tmRegistroCliente.getRowCount() == 0){
            JOptionPane.showMessageDialog(null, "Registro nao encontrado.");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        JTFRegistroCliente = new javax.swing.JTextField();
        jBPesquisa = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Registro de Cliente"));
        jPanel1.setLayout(null);
        jPanel1.add(JTFRegistroCliente);
        JTFRegistroCliente.setBounds(20, 60, 670, 30);

        jBPesquisa.setText("Pesquisar");
        jBPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisaActionPerformed(evt);
            }
        });
        jPanel1.add(jBPesquisa);
        jBPesquisa.setBounds(720, 60, 100, 30);

        jLabel1.setText("Digite o nome do cliente :");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 30, 210, 30);

        jTable1.setModel(tmRegistroCliente);
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(20, 120, 790, 90);

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 830, 280));

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(760, 300, 80, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jBPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisaActionPerformed
        DaoPessoa dp = new DaoPessoa();
        Vector<Pessoa> resultados = new Vector<Pessoa>();
        resultados = dp.buscarClientePorNome(JTFRegistroCliente.getText());
        this.mostraPesquisa(resultados);
    }//GEN-LAST:event_jBPesquisaActionPerformed

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RegistroCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RegistroCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RegistroCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RegistroCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                RegistroCliente dialog = new RegistroCliente(new javax.swing.JDialog(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JTFRegistroCliente;
    private javax.swing.JButton jBPesquisa;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
